import 'package:amway_superapp_creator_plugin/models/search_info_bean.dart';
import 'package:amway_superapp_creator_plugin/utils/toast_util.dart';
import 'package:flutter_flavor/flutter_flavor.dart';

import 'models/module_list.dart';
import 'models/user.dart';
import 'net/http_manager.dart';
import 'net/token_interceptor.dart';

class Api {
  Api() {
    var baseurl = FlavorConfig.instance.variables["baseUrl"];
    HttpManager()
        .init(baseUrl: baseurl, interceptors: [new TokenInterceptor()]);
  }

  Future<void> sensSms(String mobile) async {
    print('发送验证码...');
    await Future.delayed(Duration(seconds: 1));
    print('发送验证码成功');
  }

  /// 用户登录。简单起见，返回User
  Future<User> login(String mobile, String sms) async {
    print('登录中...');
    await Future.delayed(Duration(seconds: 1));
    User user = User(1, '程序员磊哥');
    print('登录成功: $user');
    return user;
  }

  void getHobbyList(HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback) async {
    HttpManager().get(
        url: 'apis/v1/creator/personal-configs/interest/list',
        tag: null,
        successCallback: successCallback,
        errorCallback: failureCallback);
  }

  void getIdentityList(HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback) async {
    ///此处接口的命名按照后端要求做过修改
    HttpManager().get(
        url: 'apis/v1/creator/personal-configs/identity/list',
        tag: null,
        successCallback: successCallback,
        errorCallback: failureCallback);
  }

  void getModuleList(HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback, var ada) async {
    HttpManager().get(
        url: 'apis/v1/creator/module-configs/$ada',
        tag: null,
        successCallback: successCallback,
        errorCallback: failureCallback);
  }

  void updateModuleList(ModuleData data, HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback) async {
    String id = data.id;
    if(id==null||id.isEmpty){
      ToastUtils.showShortToast('模块配置ID为空');
      return;
    }
    HttpManager().put(
        url: "apis/v1/creator/module-configs/$id",
        tag: null,
        data: data,
        successCallback: successCallback,
        errorCallback: failureCallback);
  }

  void getHaowuProductModels(HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback,var ada) async{
    HttpManager().get(url: 'apis/v1/creator/recommends/pageList/$ada/500/1/',
        tag: 'fetchRecommends',successCallback: successCallback,errorCallback: failureCallback);
  }

  void modifyRecommendList(String ada,Map<String, dynamic> map,HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback) async{
    HttpManager().post(url: 'apis/v1/creator/recommends/dataPacketUpdate/$ada',
        tag: 'updateRecommendPacket',data: map,successCallback: successCallback,errorCallback: failureCallback);
  }

  void getHaowuOfficialList(String ada,HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback) async{
    HttpManager().get(url: 'apis/v1/creator/recommends/listOfficial/$ada/', tag: 'listOfficial',successCallback: successCallback,errorCallback: failureCallback);
  }

  void getShopList(SearchInfoBean bean,HttpSuccessCallback successCallback,
      HttpFailureCallback failureCallback) async{
    HttpManager().post(url: 'apis/v1/creator/product/searchWithFilter', tag: 'searchWithFilter',data: bean,successCallback: successCallback,errorCallback: failureCallback);
  }

  void getUserIconList(HttpSuccessCallback success,HttpFailureCallback error) async{
    HttpManager().get(url: 'apis/v1/creator/personal-configs/personalSign/list', tag: null,successCallback: success,errorCallback: error);
  }
}
